<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <title>async 函数的含义和用法</title>
    </head>
    <body>
        <h1>async 函数的含义和用法</h1>
        <p>来源:<a href="http://www.ruanyifeng.com/blog/2015/05/async.html">http://www.ruanyifeng.com/blog/2015/05/async.html</a></p>
        <script>
            function timeout(ms) {
              return new Promise((resolve) => {
                setTimeout(resolve, ms);
              });
            }

            async function asyncPrint(value, ms) {
              await timeout(ms);
              console.log(value)
            }

            asyncPrint('hello world', 50);
            // await 命令后面的 Promise 对象，运行结果可能是 rejected，所以最好把 await 命令放在 try...catch 代码块中。
            async function myFunction() {
              try {
                await somethingThatReturnsAPromise();
              } catch (err) {
                console.log(err);
              }
            }
            
            // 另一种写法

            async function myFunction() {
              await somethingThatReturnsAPromise().catch(function (err){
                console.log(err);
              });
            }
        </script>
    </body>
</html>